<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>终端列表（新增终端）</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <link
      rel="stylesheet"
      href="../lib/layui-v2.5.5/css/layui.css"
      media="all"
    />
    <link rel="stylesheet" href="../css/public.css" media="all" />
  </head>
  <style>
    .layui-btn-red {
      background-color: red;
    }
    .layui-btn-Tangerine {
      background-color: chocolate;
    }
    .layui-btn-update {
      background-color: darkkhaki;
    }
    .layui-btn-atlas {
      background-color: #40b0f1e3;
    }
    .layui-btn-Msg {
      background-color: #8d4eeee3;
    }
  </style>
  <body>
    <div class="layuimini-container">
      <div class="layuimini-main">
        <div id="Myloading">
          <img
            src="../lib/layui-v2.5.5/css/modules/layer/default/loading-01.gif"
            alt=""
          />
        </div>
        <script type="text/html" id="toolbarDemo">
          <div style="color:#666; position: absolute;margin-left: 7%;"">{{d.where.name}} > {{d.where.name2}}</div>
          <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add_terminal">新增终端</button>
              <!-- <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除设备 </button> -->
          </div>
        </script>

        <table
          class="layui-hide"
          id="HcurrentTableId"
          lay-filter="currentTableFilter"
        ></table>

        <script type="text/html" id="currentTableBar">
          {{# if(d.pointType === 292){ }}
          <a
            class="layui-btn layui-btn-xs data-count-edit"
            data-title="流量计"
            layuimini-content-href="page/table/HFmeter.html"
            lay-event="Fmeter"
            >查看流量计</a
          >
          {{# }else{ }}
          <a
            class="layui-btn layui-btn-xs data-count-edit"
            data-title="实时数据"
            layuimini-content-href="page/table/HrealTime.html"
            lay-event="realTime"
            >数据</a
          >
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-danger"
            data-title="报警事项"
            layuimini-content-href="page/table/HalarmList.html"
            lay-event="delete"
            >报警</a
          >
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-atlas"
            data-title="图谱"
            layuimini-content-href="page/table/Arrester.html"
            lay-event="atlas"
            >图谱</a
          >
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-Msg"
            data-title="报文"
            layuimini-content-href="page/table/ArresterMsg.html"
            lay-event="atlasMsg"
            >报文</a
          >
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-Tangerine"
            lay-event="rename_terminal"
            >重命名</a
          >
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-red"
            lay-event="delete_terminal"
            >删除</a
          >
          {{# } }}
          <!-- 电缆测温配置 -->
          {{# if(d.pointType === 30){ }}
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-warm"
            lay-event="edit"
            >配置</a
          >
          {{# } }}
          <!-- 开关柜局放配置 -->
          {{# if(d.pointType === 0){ }}
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-update"
            lay-event="update_terminal"
            >更新</a
          >
          <!-- <a class="layui-btn layui-btn-xs data-count-edit layui-btn-warm"   lay-event="switchboard_PD">配置</a> -->
          {{# } }}
          <!-- <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a> -->
        </script>
      </div>
    </div>
    <script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
      layui.use(["form", "table", "miniTab"], function () {
        // 2020-03-25 HF sessionStorage中取出数据
        var token = sessionStorage.getItem("newtoken");
        var device = JSON.parse(sessionStorage.getItem("Savedevice"));
        var station = JSON.parse(sessionStorage.getItem("SaveStation"));

        var $ = layui.jquery,
          form = layui.form,
          miniTab = layui.miniTab,
          table = layui.table;
        miniTab.listen();

        table.render({
          elem: "#HcurrentTableId",
          url:
            "/oms-web/candt/getterminalspage?token=" +
            token +
            "&deviceCode=" +
            device.deviceCode,
          toolbar: "#toolbarDemo",
          title: "终端数据表",
          parseData: function (res) {
            //res 即为原始返回的数据
            return {
              code: res.code, //解析接口状态
              msg: res.msg, //解析提示文本
              count: res.data.total, //解析数据长度
              data: res.data.list, //解析数据列表
            };
          },
          where: { name: station.text, name2: device.text },
          defaultToolbar: [
            "filter",
            "exports",
            "print",
            {
              title: "提示",
              layEvent: "LAYTABLE_TIPS",
              icon: "layui-icon-tips",
            },
          ],

          cols: [
            [
              //复选框 {type: "checkbox", width: 50, fixed: "left"},
              { type: "numbers", width: 80, title: "序号", align: "center" },
              // {field: 'termNumber', width: 80, title: 'ID', align: "center"},
              {
                field: "text",
                minWidth: 150,
                title: "终端名称",
                align: "center",
              },
              // {field: 'deviceCode', minWidth: 110, title: '设备编号', align: "center"},
              {
                field: "channelAddr",
                width: 150,
                title: "网关编号",
                align: "center",
              },
              {
                field: "terminalCode",
                width: 190,
                title: "终端编号",
                align: "center",
              },
              {
                field: "pointType",
                width: 150,
                title: "终端类型",
                align: "center",
                templet: function (res) {
                  return res.typeText;
                },
              },
              { field: "status", width: 120, title: "状态", align: "center" },
              {
                title: "操作",
                width: 300,
                templet: "#currentTableBar",
                fixed: "right",
                align: "center",
              },
            ],
          ],
          limits: [10, 15, 20, 25, 50, 100],
          limit: 15,
          page: true,
          done: function (res, curr, count) {
            // res 数据  curr 点的是第几个 count 总共多少个数据
            if (res.code == -1) {
              window.location = "./login-1.html";
            }
            //回调函数判断数据中断背景变色

            // 保存数据  作为曲线的名称
            $("#Myloading").remove();
          },
        });

        //缓存当前操作的是哪个表格的哪个tr的哪个td
        $(document)
          .off("mousedown", ".layui-table-grid-down")
          .on("mousedown", ".layui-table-grid-down", function (event) {
            //直接记录td的jquery对象
            table._tableTrCurrr = $(this).closest("td");
          });

        //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
        $(document)
          .off("click", ".layui-table-tips-main [lay-event]")
          .on("click", ".layui-table-tips-main [lay-event]", function (event) {
            var elem = $(this);
            var tableTrCurrr = table._tableTrCurrr;
            if (!tableTrCurrr) {
              return;
            }
            var layerIndex = elem.closest(".layui-table-tips").attr("times");
            layer.close(layerIndex);
            table._tableTrCurrr
              .find('[lay-event="' + elem.attr("lay-event") + '"]')
              .first()
              .click();
          });

        // 监听搜索操作
        form.on("submit(data-search-btn)", function (data) {
          var result = JSON.stringify(data.field);
          layer.alert(result, {
            title: "最终的搜索信息",
          });

          //执行搜索重载
          table.reload(
            "currentTableId",
            {
              page: {
                curr: 1,
              },
              where: {
                searchParams: result,
              },
            },
            "data"
          );

          return false;
        });

        /**
         * toolbar监听事件
         */
        table.on("toolbar(currentTableFilter)", function (obj) {
          if (obj.event === "add_terminal") {
            // 监听添加操作
            var index = layer.open({
              title: "添加终端",
              type: 2,
              shade: 0.2,
              resize: false,
              // maxmin:true,//开启最大化最小化按钮
              shadeClose: true,
              skin: "layui-layer-molv",
              offset: ["80px"],
              area: ["40%", "52%"],
              // area: ['450px', '390px'],
              // content: './table/add_terminal.html',
              content: "./table/add_terminal.html",
            });
            // $(window).on("resize", function () {
            //     layer.full(index);
            // });
          } else if (obj.event === "delete") {
            // 监听删除操作
            var checkStatus = table.checkStatus("currentTableId"),
              data = checkStatus.data;
            layer.alert(JSON.stringify(data));
          }
        });

        //监听表格复选框选择
        table.on("checkbox(currentTableFilter)", function (obj) {
          console.log(obj);
        });

        // 获取列表data
        table.on("tool(currentTableFilter)", function (obj) {
          // 2020-03-25 HF sessionStorage存储所需接口参数
          var data = obj.data;

          /***********获取实时数据****************/
          function getRealTime(data) {
            $.ajax({
              url:
                "/oms-web/point-table/getPointTable?deviceCode=" +
                data.deviceCode +
                "&termNo=" +
                data.termNumber +
                "&token=" +
                token,
              dataType: "json",
              type: "POST",
              async: false,
              contentType: "application/json",
              success: function (data) {
                if (data.code == -1) {
                  window.location = "./login-1.html";
                }
                if (data.code === 0) {
                  //    置空数组
                  var Htelemeterings = [];
                  if (data.data != null && data.data != undefined) {
                    // 2020-03-25 HF 保存遥信数据
                    for (let i = 0; i < data.data.telemeterings.length; i++) {
                      Htelemeterings.push(data.data.telemeterings[i].code);
                    }
                    // 2020-03-25 HF 保存遥测数据
                    for (let i = 0; i < data.data.telesignallings.length; i++) {
                      Htelemeterings.push(data.data.telesignallings[i].code);
                    }
                    // 2020-03-25 HF 保存sessionStorage
                    sessionStorage.setItem(
                      "SaveHtelemeterings",
                      JSON.stringify(Htelemeterings)
                    );

                    sessionStorage.setItem(
                      "SaveTelemeterings",
                      JSON.stringify(data.data.telemeterings)
                    );
                    sessionStorage.setItem(
                      "SaveTelePtss",
                      JSON.stringify(data.data.telePtss)
                    );
                  }
                }
              },
              error: function () {
                layer.msg("数据出错！");
              },
            });
          }

          /****************获取无线测温数据**********************/
          function GetConfigre(data) {
            $.ajax({
              url:
                "/oms-web/wirelessTemp/getConfigure?rtuno=" +
                data.termNumber +
                "&token=" +
                token,
              dataType: "json",
              type: "get",
              async: false,
              contentType: "application/json",
              success: function (data) {
                if (data.code == -1) {
                  window.location = "./login-1.html";
                }
                if (data.code === 0) {
                  if (data.data != null && data.data != undefined) {
                    sessionStorage.setItem(
                      "SaveConfigData",
                      JSON.stringify(data.data)
                    );
                  }
                }
              },
              error: function () {
                layer.msg("数据出错！", { time: 1000 });
              },
            });
          }

          if (obj.event === "edit") {
            GetConfigre(data);
            sessionStorage.setItem("SaveConfigName", JSON.stringify(data));
            var index = layer.open({
              title: "配置设备",
              type: 2,
              resize: false,
              skin: "layui-layer-molv",
              shade: 0.4,
              maxmin: false, //开启最大化最小化按钮
              shadeClose: false,
              area: ["35%", "62%"],
              // btn:['确认提交'],
              content: ["./table/Hconfig.html"],
              success: function () {},
              cancel: function () {
                sessionStorage.removeItem("SaveConfigName");
                sessionStorage.removeItem("SaveConfigData");
              },
            });
            // $(window).on("resize", function () {
            //     layer.full(index);
            // });
            return false;
          } else if (obj.event === "delete") {
            var alarmRequest = {
              Req: 3,
              termNumber: obj.data.termNumber,
            };

            sessionStorage.setItem("SaveRequest", JSON.stringify(alarmRequest));
          } else if (obj.event === "realTime") {
            getRealTime(data);
          } else if (obj.event === "Fmeter") {
            sessionStorage.setItem("SaveFmeter", JSON.stringify(data));
          } else if (obj.event === "delete_terminal") {
            var fg = {};
            layer.confirm(
              "确定删除该终端吗？",
              {
                btn: ["确定", "取消"], //按钮
              },
              function () {
                $.ajax({
                  url:
                    "/oms-web/candt/delete-terminal/" +
                    obj.data.termNumber +
                    "?token=" +
                    token,
                  // url: '/oms-web/delete-terminal/'+obj.data.termNumber+'?token='+ token  ,
                  dataType: "json",
                  type: "post",
                  contentType: "application/json",
                  success: function (data) {
                    if (data.code == -1) {
                      window.location = "./login-1.html";
                    }
                    if (data.code === 0) {
                      layer.closeAll();
                      layer.msg(
                        "删除成功！",
                        { icon: 1, time: 1000 },
                        function () {
                          window.location.reload();
                        }
                      );
                    } else {
                      layer.closeAll();
                      layer.msg(data.msg, { icon: 2, time: 1000 });
                    }
                  },
                  error: function () {
                    layer.msg("数据请求错误！");
                    layer.closeAll();
                  },
                });
              }
            );
          } else if (obj.event === "atlas") {
            var atlasRequest = {
              termNumber: obj.data.termNumber,
              text: obj.data.text,
            };
            sessionStorage.setItem("PicRequest", JSON.stringify(atlasRequest));
          } else if (obj.event === "atlasMsg") {
            var atlasMsgRequest = {
              termNumber: obj.data.termNumber,
              text: obj.data.text,
            };
            sessionStorage.setItem(
              "PicMsgRequest",
              JSON.stringify(atlasMsgRequest)
            );
            console.log(obj.data.text);
          } else if (obj.event === "rename_terminal") {
            console.log(obj);
            var index = layer.open({
              title: "重命名终端",
              type: 1,
              shade: 0.2,
              // maxmin:true,
              shadeClose: true,
              offset: ["150px"],
              skin: "layui-layer-molv",
              area: ["350px", "200px"],
              content:
                '<div style="margin-top:20px;"><p style="margin-bottom: 15px;margin-left: 5px;">新终端名称：</p><input AUTOCOMPLETE="off" type="text" name="rename_terminal" lay-verify="required" lay-reqtext="当前站点不能为空" value="' +
                obj.data.text +
                '"  class="layui-input"></div>',
              btn: ["确认"],
              yes: function (index, layero) {
                var terminal_name = $("input[name=rename_terminal]")
                  .val()
                  .trim()
                  .replace(/\s/g, ""); //获取多行文本框的值
                // 检测是否名字重复
                if (obj.data.text == terminal_name) {
                  layer.msg("重命名重复！");
                  return;
                }
                // *************新增站点接口*******************//
                layer.confirm(
                  "确定修改该终端名吗？",
                  {
                    btn: ["确定", "取消"], //按钮
                  },
                  function () {
                    $.ajax({
                      url:
                        "/oms-web/candt/rename-terminal/" +
                        obj.data.terminalCode +
                        "?token=" +
                        token +
                        "&terminalName=" +
                        terminal_name +
                        "&terminalNo=" +
                        obj.data.termNumber,
                      dataType: "json",
                      type: "post",
                      contentType: "application/json",
                      success: function (data) {
                        if (data.code == -1) {
                          window.location = "./login-1.html";
                        }
                        if (data.code === 0) {
                          layer.close(index);
                          layer.msg(
                            "修改成功！",
                            { icon: 1, time: 1000 },
                            function () {
                              window.location.reload();
                            }
                          );
                        } else {
                          layer.close(index);
                          layer.msg(data.msg, { icon: 1, time: 1000 });
                        }
                      },
                      error: function () {
                        layer.msg("数据请求错误！");
                        layer.close(index);
                      },
                    });
                  }
                );
              },
            });
          } else if (obj.event === "update_terminal") {
            var index = layer.open({
              title: "更新网关、终端",
              type: 1,
              shade: 0.2,
              // maxmin:true,
              shadeClose: true,
              offset: ["150px"],
              skin: "layui-layer-molv",
              area: ["400px", "210px"],
              content:
                '<div style="margin-top:20px;"><p style="margin-bottom: 15px;margin-left: 5px;">更新网关编号：</p><input  type="text" name="rename_candt" lay-verify="required" lay-reqtext="网关编号不能为空" value="' +
                obj.data.channelAddr +
                '"  class="layui-input"></div>',
              btn: ["确认"],
              yes: function (index, layero) {
                var candt_code = $("input[name=rename_candt]")
                  .val()
                  .trim()
                  .replace(/\s/g, ""); //获取多行文本框的值
                var terminal_code = obj.data.terminalCode;
                // 检测是否名字重复
                if (obj.data.channelAddr == candt_code) {
                  layer.msg("网关、终端名并未作出改变！");
                  return;
                }
                // *************新增站点接口*******************//
                layer.confirm(
                  "确定更新该网关或者终端吗？",
                  {
                    btn: ["确定", "取消"], //按钮
                  },
                  function () {
                    $.ajax({
                      url:
                        "/oms-web/candt/binding?token=" +
                        token +
                        "&channelCode=" +
                        candt_code +
                        "&terminalNo=" +
                        obj.data.termNumber +
                        "&terminalCode=" +
                        terminal_code,
                      dataType: "json",
                      type: "post",
                      contentType: "application/json",
                      success: function (data) {
                        if (data.code == -1) {
                          window.location = "./login-1.html";
                        }
                        if (data.code === 0) {
                          layer.close(index);
                          layer.msg(
                            "修改成功！",
                            { icon: 1, time: 1000 },
                            function () {
                              window.location.reload();
                            }
                          );
                        } else {
                          layer.close(index);
                          layer.msg(data.msg, { icon: 1, time: 1000 });
                        }
                      },
                      error: function () {
                        layer.msg("数据请求错误！");
                        layer.close(index);
                      },
                    });
                  }
                );
              },
            });
          }
        });
      });
    </script>
    <script></script>
  </body>
</html>
